from django.db import models

class ToplevelManager(models.Manager):
    def get_query_set(self):
        return super(ToplevelManager,self).get_query_set().filter(parent__isnull=True).order_by('title')

class BottomlevelManager(models.Manager):
    def get_query_set(self):
        return super(BottomlevelManager,self).get_query_set().filter(parent__isnull=False).order_by('title')

class Extra(models.Model):
    pagename = models.SlugField(max_length=10,unique=True)
    title = models.CharField(max_length=100)
    content = models.TextField()
    parent = models.ForeignKey('self', null=True, blank=True)

    objects = models.Manager()
    tops = ToplevelManager()
    bottoms = BottomlevelManager()
    
    class Meta:
        ordering = ['title']

    class Admin:
        list_display = ('pagename', 'title')

    def __unicode__(self):
        return u"%s (%s)" % (self.title, self.pagename)

    @models.permalink
    def get_absolute_url(self):
        if self.parent:
            return ('nameremoved.extra.views.double', (),
                    {'which_main': self.parent.pagename,
                     'which_second': self.pagename})
        else:
            return ('nameremoved.extra.views.single', (),
                    {'which_main': self.pagename})
